﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LuDo.PitEditor
{
    public partial class Filtering : Form
    {
        public string FilteringString { get; private set; }
        public Filtering(string CurrentString)
        {
            InitializeComponent();
            UpdateGUI(CurrentString);
        }

        private void UpdateGUI(string CurrentString)
        {
            bool bozze, pub, periodo;
            DateTime Date1, Date2;
            ManageFilter(CurrentString, out bozze, out pub, out periodo, out Date1, out Date2);
            cbMostraBozze.Checked = bozze;
            cbMostraPubblicati.Checked = pub;
            if (!periodo)
            {
                rbPeriodo1.Checked = true;
            }
            else
            {
                int span = Convert.ToInt32(Math.Abs(Date1.Subtract(Date2).TotalDays));
                switch (span)
                {
                    case 7: rbPeriodo2.Checked = true;
                        break;
                    case 30: rbPeriodo3.Checked = true;
                        break;
                    default: rbPeriodo4.Checked = true;
                        break;
                }
            }
            cbMostraBozze_CheckedChanged(null, null);
            cbMostraPubblicati_CheckedChanged(null, null);
            PeriodoCheckChanged(null, null);
        }
        private void cbMostraBozze_CheckedChanged(object sender, EventArgs e)
        {
            if (!cbMostraBozze.Checked && !cbMostraPubblicati.Checked)
            {
                cbMostraPubblicati.Checked = true;
                gbPeriodo.Show();
            }
        }
        private void cbMostraPubblicati_CheckedChanged(object sender, EventArgs e)
        {
            if (!cbMostraBozze.Checked && !cbMostraPubblicati.Checked)
            {
                cbMostraBozze.Checked = true;
            } 
            gbPeriodo.Visible = cbMostraPubblicati.Checked;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            DateTime BeginDate = DateTime.MinValue;
            if (rbPeriodo2.Checked)
            {
                BeginDate = DateTime.Today.AddDays(-7);
            }
            else if (rbPeriodo3.Checked)
            {
                BeginDate = DateTime.Today.AddDays(-30);
            }
            else if (rbPeriodo4.Checked)
            {
                double span = Convert.ToDouble(udGiorni.Value);
                BeginDate = DateTime.Today.AddDays(-span);
            }
            this.FilteringString = GeneraFilterString(cbMostraBozze.Checked,cbMostraPubblicati.Checked,!rbPeriodo1.Checked,BeginDate,DateTime.Today);
            if (cbDefault.Checked)
            {
                PitEditor.Properties.Settings.Default.DefaultFilter = this.FilteringString;
                PitEditor.Properties.Settings.Default.Save();
            }
        }
        private void PeriodoCheckChanged(object sender, EventArgs e)
        {
            pnlGiorni.Visible = rbPeriodo4.Checked;
        }

        //Static methods
        static private void ManageFilter(string FilterString, out bool _bozze, out bool _pub, out bool _periodo, out DateTime _d1, out DateTime _d2)
        {
            _bozze = FilterString.Contains("DataUpload IS NULL");
            _pub = FilterString.Contains("DataUpload IS NOT NULL");
            _periodo = FilterString.Contains(">=");
            _d1 = DateTime.MinValue;
            _d2 = DateTime.MinValue;
            if (_periodo)
            {
                int posData1 = FilterString.IndexOf(">=") + 4;
                int posData2 = FilterString.IndexOf("<=") + 4;
                _d1 = Convert.ToDateTime(FilterString.Substring(posData1, 10));
                _d2 = Convert.ToDateTime(FilterString.Substring(posData2, 10));
            }
        }
        static public string ExplainFilter(string CurrentString)
        {
            string str = "";

            bool bozze, pub, periodo;
            DateTime Date1, Date2;
            ManageFilter(CurrentString, out bozze, out pub, out periodo, out Date1, out Date2);
            if (!pub)
            {
                str = "Solo bozze";
            }
            else
            {
                if (bozze)
                {
                    str = "Bozze e ";
                }
                if (!periodo)
                {
                    str += "tutti pubblicati";
                }
                else
                {
                    str += String.Format("pubblicati nel periodo dal {0:d} al {1:d}", Date1, Date2);
                }
            }
            return str;
        }
        static public string GeneraFilterString(bool Bozze, bool Pubblicati, bool Periodo, DateTime Data1, DateTime Data2)
        {
            string ftr = "";
            string stato1 = "FALSE";
            string stato2 = "FALSE";
            string periodo = "TRUE";

            if (Bozze)
            {
                stato1 = "DataUpload IS NULL";
            }
            if (Pubblicati)
            {
                string campo = "DataUpload";
                stato2 = "DataUpload IS NOT NULL";
                if (!Periodo)
                {
                    periodo = "TRUE";
                }
                else
                {
                    int span = Convert.ToInt32(Math.Abs(Data1.Subtract(Data2).TotalDays));
                    DateTime DataStart = DateTime.Today.AddDays(-span);
                    DateTime BeginDate = new DateTime(DataStart.Year, DataStart.Month, DataStart.Day, 0, 0, 0);
                    DateTime EndDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 23, 59, 59);
                    periodo = String.Format("{0} >= '{1:G}' AND {0} <= '{2:G}'", campo, BeginDate, EndDate);
                }
            }

            ftr = String.Format("{0} OR ({1} AND {2})", stato1, stato2, periodo);
            return ftr;
        }
        static public string GeneraFilterString(string FilterString)
        {
            bool bozze, pub, periodo;
            DateTime Date1, Date2;
            ManageFilter(FilterString, out bozze, out pub, out periodo, out Date1, out Date2);
            return GeneraFilterString(bozze, pub, periodo, Date1, Date2);
        }    
    }
}
